home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / checkbox / properties.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  3.9 KB  |  131 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. from checkbox.attribute import Attribute
  20. from checkbox.variables import (BoolVariable, StringVariable,
  21.     PathVariable, IntVariable, FloatVariable, ListVariable,
  22.     VariableFactory, Variable, get_variable)
  23.  
  24.  
  25. class Property(object):
  26.  
  27.     def __init__(self, variable_class=Variable, variable_kwargs={}):
  28.         self._variable_class = variable_class
  29.         self._variable_kwargs = variable_kwargs
  30.  
  31.     def __get__(self, obj, cls=None):
  32.         if obj is None:
  33.             return self._get_attribute(cls)
  34.         if cls is None:
  35.             cls = type(obj)
  36.         attribute = self._get_attribute(cls)
  37.         variable = get_variable(obj, attribute)
  38.         return variable.get()
  39.  
  40.     def __set__(self, obj, value):
  41.         cls = type(obj)
  42.         attribute = self._get_attribute(cls)
  43.         variable = get_variable(obj, attribute)
  44.         variable.set(value)
  45.  
  46.     def _detect_name(self, used_cls):
  47.         self_id = id(self)
  48.         for cls in used_cls.__mro__:
  49.             for attr, prop in cls.__dict__.items():
  50.                 if id(prop) == self_id:
  51.                     return attr
  52.         raise RuntimeError("Property used in an unknown class")
  53.  
  54.     def _get_attribute(self, cls):
  55.         try:
  56.             attribute = cls.__dict__["_checkbox_attributes"].get(self)
  57.         except KeyError:
  58.             cls._checkbox_attributes = {}
  59.             attribute = None
  60.  
  61.         if attribute is None:
  62.             name = self._detect_name(cls)
  63.             attribute = PropertyAttribute(self, cls, name,
  64.                 self._variable_class, self._variable_kwargs)
  65.             cls._checkbox_attributes[self] = attribute
  66.  
  67.         return attribute
  68.  
  69.  
  70. class PropertyAttribute(Attribute):
  71.  
  72.     def __init__(self, prop, cls, name, variable_class, variable_kwargs):
  73.         super(PropertyAttribute, self).__init__(name, cls,
  74.             VariableFactory(variable_class, attribute=self, **variable_kwargs))
  75.  
  76.         self.cls = cls # Used by references
  77.  
  78.         # Copy attributes from the property to avoid one additional
  79.         # function call on each access.
  80.         for attr in ["__get__", "__set__"]:
  81.             setattr(self, attr, getattr(prop, attr))
  82.  
  83.  
  84. class PropertyType(Property):
  85.  
  86.     def __init__(self, **kwargs):
  87.         kwargs["value"] = kwargs.pop("default", None)
  88.         kwargs["value_factory"] = kwargs.pop("default_factory", None)
  89.         super(PropertyType, self).__init__(self.variable_class, kwargs)
  90.  
  91.  
  92. class Bool(PropertyType):
  93.  
  94.     variable_class = BoolVariable
  95.  
  96.  
  97. class String(PropertyType):
  98.  
  99.     variable_class = StringVariable
  100.  
  101.  
  102. class Path(PropertyType):
  103.  
  104.     variable_class = PathVariable
  105.  
  106.  
  107. class Int(PropertyType):
  108.  
  109.     variable_class = IntVariable
  110.  
  111.  
  112. class Float(PropertyType):
  113.  
  114.     variable_class = FloatVariable
  115.  
  116.  
  117. class List(PropertyType):
  118.  
  119.     variable_class = ListVariable
  120.  
  121.     def __init__(self, **kwargs):
  122.         if "default" in kwargs:
  123.             raise ValueError("'default' not allowed for List. "
  124.                              "Use 'default_factory' instead.")
  125.         type = kwargs.pop("type", None)
  126.         if type is None:
  127.             type = Property()
  128.         kwargs["item_factory"] = VariableFactory(type._variable_class,
  129.                                                  **type._variable_kwargs)
  130.         super(List, self).__init__(**kwargs)
  131.